<?php
/**
 * Desc: Service模板
 * User: zdb
 * Date: 2023/6/5
 * Time: 11:22
 */

namespace app\api\service\resource;
use think\facade\Db;

class GeoFoodService
{

    private $primaryTable;

    public function __construct()
    {
        $this->primaryTable = "yj_geo_food";
    }

    public function findRowById($id)
    {
        return Db::table($this->primaryTable)->alias('a')
            ->where('id', '=', $id)
            ->strict(false)
            ->find();
    }

    public function findRowByData($data)
    {
        $condition = $this->assembleFilterCondition($data);
        return Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->strict(false)
            ->find();
    }

    public function getListByManyId($arrId)
    {
        $list = Db::table($this->primaryTable)->alias('a')
            ->whereIn('id', $arrId)
            ->select()
            ->toArray();
        return $list;
    }

    public function assembleFilterCondition($data)
    {
        if (!empty($data['title'])) {
            $condition[] = "title like '%" . $data['title'] . "%'";
        }

        if (!empty($data['example_id'])) {
            $condition[] = "example_id =".$data['example_id'];
        }

        if (!empty($data['city_id'])) {
            $condition[] = "gc.id =".$data['city_id'];
        }

        if (!empty($data['area_id'])) {
            $condition[] = "ga.id =".$data['area_id'];
        }

        return empty($condition) ? "" : implode(" AND ", $condition);
    }

    public function getListNoPage($data = [], $limitNum = 30)
    {
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)->alias('a')
            ->leftJoin("yj_food f", "f.id = a.food_id")
            ->leftJoin("yj_geo_city gc", "gc.city_code = a.city_code")
            ->leftJoin("yj_geo_area ga", "ga.area_code = a.area_code")
            ->where($condition)
            ->limit(0, $limitNum)
            ->order('a.id desc')
            ->field("a.id, a.province_code, a.city_code, a.area_code, a.create_time, a.update_time, f.food_name")
            ->select()
            ->toArray();
        return $res;
    }

    public function getListWithPage($data, $pageIndex = 1, $pageSize = 30)
    {
        $offset = ($pageIndex - 1) * $pageSize;
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->limit($offset, $pageSize)
            ->order('a.id desc')
            ->select();
        return $res;
    }

    public function getTotalNum($data)
    {
        $condition = $this->assembleFilterCondition($data);
        $num = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->count();
        return $num;
    }

    public function createRow($data)
    {
        $data['status'] = isset($data['status']) ? $data['status']:1;
        $res = Db::table($this->primaryTable)
            ->strict(false)
            ->duplicate($data)
            ->insert($data, true);
        return $res;
    }

    public function createManyRow($data)
    {
        $res = Db::table($this->primaryTable)
            ->strict(false)
            ->duplicate($data)
            ->insertAll($data, true);
        return $res;
    }

    public function updateRowById($id, $data)
    {
        $res = Db::table($this->primaryTable)->alias('a')
            ->where('id', '=', $id)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function updateListByData($condition, $data)
    {
        $condition = $this->assembleFilterCondition($condition);
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function updateListByManyId($arrId, $data)
    {
        $res = Db::table($this->primaryTable)->alias('a')
            ->whereIn('a.id', $arrId)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeRowById($id)
    {
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)
            ->where('id', '=', $id)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeListByManyId($arrId)
    {
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)->alias('a')
            ->whereIn('a.id', $arrId)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeListByData($data)
    {
        $condition = $this->assembleFilterCondition($data);
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function addNewGeoFood($data)
    {
        $serviceFood = new FoodService();
        $foodInfo = $serviceFood->findRowByData(array('food_name' => $data['food_name']));
        if($foodInfo){
            $newFoodId = $foodInfo['id'];
        }else{
            $foodData = array('food_name' => $data['food_name']);
            $newFoodId = $serviceFood->createRow($foodData);
        }
        if(!empty($data['area_id'])){
            $serviceGeoArea = new GeoAreaService();
            $areaInfo = $serviceGeoArea->findRowById($data['area_id']);
            $newGeoFoodData = array(
                'food_id' => $newFoodId,
                'province_code' => $areaInfo['province_code'],
                'city_code' => $areaInfo['city_code'],
                'area_code' => $areaInfo['area_code'],
            );
        }else{
            $serviceGeoCity = new GeoCityService();
            $cityInfo = $serviceGeoCity->findRowById($data['city_id']);
            $newGeoFoodData = array(
                'food_id' => $newFoodId,
                'province_code' => $cityInfo['province_code'],
                'city_code' => $cityInfo['city_code'],
                'area_code' => '',
            );
        }

        return $this->createRow($newGeoFoodData);
    }
}